package cn.iocoder.yudao.framework.pay.core.client.impl.yyf;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderRespDTO;
import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderUnifiedReqDTO;
import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundRespDTO;
import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundUnifiedReqDTO;
import cn.iocoder.yudao.framework.pay.core.client.dto.transfer.PayTransferRespDTO;
import cn.iocoder.yudao.framework.pay.core.client.dto.transfer.PayTransferUnifiedReqDTO;
import cn.iocoder.yudao.framework.pay.core.client.impl.AbstractPayClient;
import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum;
import cn.iocoder.yudao.framework.pay.core.enums.order.PayOrderDisplayModeEnum;
import cn.iocoder.yudao.framework.pay.core.enums.transfer.PayTransferTypeEnum;
import com.xingyuv.captcha.util.MD5Util;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

@Slf4j
public class YYFPayClient  extends AbstractPayClient<YYFPayConfig> {



    private static final String YYF_RESP_SUCCESS_DATA = "YYF_SUCCESS";
    ErrorCode THERE_CODE = new ErrorCode(1_007_901_004, "第三方支付报错{}");

    public YYFPayClient(Long channelId, YYFPayConfig config) {
        super(channelId, PayChannelEnum.YYF.getCode(), config);
    }

    /**
     * 自定义初始化
     */
    @Override
    protected void doInit() {

    }

    /**
     * 发送三方请求 todo 三方支付
     * @param reqDTO
     * @return
     * @throws Throwable
     */
    @Override
    protected PayOrderRespDTO doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) throws Throwable {

        String fileUrl = "https://pay.unbb.cn/submit.php?";
        String pid= this.config.getPid();
//        String pid= "124982590";

        String type= reqDTO.getChannelExtras().get("channelCode");
        String out_trade_no= reqDTO.getOutTradeNo();
        String notify_url= reqDTO.getNotifyUrl();
        String return_url= "https://pay.unbb.cn/User/SDK/type_return_url.php";
        String name= "优云付订单";
        String money= reqDTO.getPrice().toString();
        String  sitename= "优云付";
        String sign_type= "MD5";
        String md5Key= this.config.getMd5Key();
        String signs=  "money="+money
                +"&name="+name
                +"&notify_url="+notify_url
                +"&out_trade_no="+out_trade_no
                +"&pid="+pid
                +"&return_url="+return_url
                +"&sitename="+sitename
                +"&type="+type+md5Key;
//                +"&key="+md5Key;
        String sign = MD5Util.md5(signs);
        fileUrl = fileUrl+"pid="+pid+"&type="+type
                +"&out_trade_no="+out_trade_no
                +"&notify_url="+notify_url
                +"&return_url="+return_url
                +"&name="+name
                +"&money="+money
                +"&sitename="+sitename
                +"&sign="+sign
                +"&sign_type="+sign_type;
        HttpResponse execute1 = HttpUtil.createGet(fileUrl).execute();

        String displayMode = ObjectUtil.defaultIfNull(reqDTO.getDisplayMode(),
                PayOrderDisplayModeEnum.URL.getMode());
        //response.getBody() 跳转地址
        String s = execute1.body().toString();
        String prifix = "window.location.href='";
        String suffix ="';";
        String s1 = Arrays.toString(StrUtil.subBetweenAll(s, prifix, suffix));
        String sub = StrUtil.sub(s1, 1, -1);
        if (StrUtil.isEmpty(sub)){
            String prifix1 = "<body id=\"error-page\">\n" +
                    "        <h3>站点提示信息</h3>";
            String suffix1 ="</body>";
            String error = Arrays.toString(StrUtil.subBetweenAll(s, prifix1, suffix1));
            throw  exception(THERE_CODE,error);
        }

        return PayOrderRespDTO.waitingOf(displayMode, "https://pay.unbb.cn/"+sub,
                reqDTO.getOutTradeNo(), execute1);
    }

    @Override
    protected PayOrderRespDTO doParseOrderNotify(Map<String, String> params, String body) throws Throwable {
        //TODO
        throw new UnsupportedOperationException("模拟支付无支付回调");
    }

    @Override
    protected PayOrderRespDTO doGetOrder(String outTradeNo) throws Throwable {
        return PayOrderRespDTO.successOf("YYF-P-" + outTradeNo, "", LocalDateTime.now(),
                outTradeNo, YYF_RESP_SUCCESS_DATA);
    }

    @Override
    protected PayRefundRespDTO doUnifiedRefund(PayRefundUnifiedReqDTO reqDTO) throws Throwable {
        return PayRefundRespDTO.successOf("YYF-R-" + reqDTO.getOutRefundNo(), LocalDateTime.now(),
                reqDTO.getOutRefundNo(), YYF_RESP_SUCCESS_DATA);
    }

    @Override
    protected PayRefundRespDTO doParseRefundNotify(Map<String, String> params, String body) throws Throwable {
        throw new UnsupportedOperationException("模拟支付无退款回调");
    }

    @Override
    protected PayRefundRespDTO doGetRefund(String outTradeNo, String outRefundNo) throws Throwable {
        return PayRefundRespDTO.successOf("YYF-R-" + outRefundNo, LocalDateTime.now(),
                outRefundNo, YYF_RESP_SUCCESS_DATA);
    }

    @Override
    protected PayTransferRespDTO doUnifiedTransfer(PayTransferUnifiedReqDTO reqDTO) throws Throwable {
        throw new UnsupportedOperationException("待实现");
    }

    @Override
    protected PayTransferRespDTO doGetTransfer(String outTradeNo, PayTransferTypeEnum type) throws Throwable {
        throw new UnsupportedOperationException("待实现");
    }


}
